package edu.ynu.se.xiecheng.achitectureclass.controller;

import edu.ynu.se.xiecheng.achitectureclass.common.controller.LogicController;
import edu.ynu.se.xiecheng.achitectureclass.dao.DeviceDao;
import edu.ynu.se.xiecheng.achitectureclass.entity.Device;
import edu.ynu.se.xiecheng.achitectureclass.service.DeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@Api(tags = "设备管理接口")
@RestController
@RequestMapping("/device")
public class DeviceController extends LogicController<DeviceService, DeviceDao, Device, Long> {

    public DeviceController(@Autowired DeviceService service) {
        super(service);
    }

    private Map<String, Object> formatDeviceData(Device device) {
        Map<String, Object> deviceData = new HashMap<>();
        deviceData.put("id", device.getId());
        deviceData.put("name", device.getName());
        deviceData.put("serialNumber", device.getSerialNumber());
        deviceData.put("boxNo", device.getBoxNo());
        deviceData.put("deviceType", device.getDeviceType());
        deviceData.put("unit", device.getUnit());
        deviceData.put("monitorUrl", device.getMonitorUrl());
        deviceData.put("createTime", device.getCreateTime());
        deviceData.put("updateTime", device.getUpdateTime());
        return deviceData;
    }

    @GetMapping("/info")
    @ApiOperation("获取单个设备信息")
    public ResponseEntity<?> getDeviceInfo(@ApiParam("设备ID") @RequestParam Long id) {
        try {
            Device device = getService().getDeviceInfo(id);
            return ResponseEntity.ok(Map.of(
                    "code", 200,
                    "message", "查询成功",
                    "data", formatDeviceData(device)
            ));
        } catch (Exception e) {
            return ResponseEntity.ok(Map.of(
                    "code", 400,
                    "message", e.getMessage()
            ));
        }
    }

    @PostMapping("/update")
    @ApiOperation("更新设备信息")
    public ResponseEntity<?> updateDevice(@Valid @RequestBody Device device) {
        try {
            Device updatedDevice = getService().updateDevice(device);
            return ResponseEntity.ok(Map.of(
                    "code", 200,
                    "message", "更新成功",
                    "data", formatDeviceData(updatedDevice)
            ));
        } catch (Exception e) {
            return ResponseEntity.ok(Map.of(
                    "code", 400,
                    "message", e.getMessage()
            ));
        }
    }

    @PostMapping("/save")
    @ApiOperation("保存设备信息")
    public ResponseEntity<?> saveDevice(@Valid @RequestBody Map<String, Object> requestData) {
        try {
            Device device = new Device();
            device.setName((String) requestData.get("name"));
            device.setSerialNumber((String) requestData.get("serialNumber"));
            device.setBoxNo((String) requestData.get("boxNo"));
            device.setDeviceType((String) requestData.get("deviceType"));
            device.setUnit((String) requestData.get("unit"));
            device.setMonitorUrl((String) requestData.get("monitorUrl"));
            
            Long greenhouseId = Long.valueOf(requestData.get("greenhouse_id").toString());
            
            Device savedDevice = getService().saveDevice(device, greenhouseId);
            return ResponseEntity.ok(Map.of(
                    "code", 200,
                    "message", "保存成功",
                    "data", formatDeviceData(savedDevice)
            ));
        } catch (Exception e) {
            return ResponseEntity.ok(Map.of(
                    "code", 400,
                    "message", e.getMessage()
            ));
        }
    }

    @PostMapping("/initialize-sensors")
    @ApiOperation("初始化传感器设备")
    public ResponseEntity<?> initializeSensors(@RequestParam Long greenhouseId) {
        try {
            getService().initializeSensors(greenhouseId);
            return ResponseEntity.ok(Map.of(
                    "code", 200,
                    "message", "传感器初始化成功"
            ));
        } catch (Exception e) {
            return ResponseEntity.ok(Map.of(
                    "code", 400,
                    "message", e.getMessage()
            ));
        }
    }
} 